<?
if (preg_match("#" . basename(__FILE__) . "#", $_SERVER['PHP_SELF'])) {
	die('This page cannot be called directly.') ;
}

class ajax_location {
	function playme(){
        $code = Url::getParam('code');
        //yêu cầu đặt tên $code=tên của method trong class
        if (method_exists($this, $code)) {
            $this->$code();
        }else{
            $this->home();
        }
	}
	
	function home(){
		global $display;
		die("Nothing to do...");
	}
	
	function del_location(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
        if(User::have_permit("location_admin")==false){
            echo "no_perm";
            exit;
        }

		$location_id= (int)Url::get("id");
		if($location_id){
			$location 	= Db_Location::getLocationById($location_id);
		}else{
			$location=array();
		}
		if($location){//check quyền và ks có tồn tại
			if(Db_Location::delLocationById($location_id)){
                echo "success";
            }else{
                echo "un_success";
            }
		}else{
			echo "no_perm";
		}
	}

    function del_locations_selected(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
        if(User::have_permit("location_admin")==false){
            echo "no_perm";
            exit;
        }
        
		$location_ids= Url::get("selected_ids");
		if(is_array($location_ids)){
            if(!Db_Location::delLocations($location_ids)){
                echo "un_success";
            }else{
                echo "success";
            }
		}else{
            echo "not_item";
        }
	}

    function getItem(){ /*Hàm sử dụng trong manage_prior_hotel - Tú 09.01.13*/
        $hotel = Url::getInt('hotel');
        $type = Url::get('type');
        $text = '';
        if ($type == 'deal'){//Lấy về deal==================================================================
            $deals = Db_Deal::getDealHotelAvailable('*', '', ' hotel_id='.$hotel,'');
            $text = '<th>Chọn deal:</th><td><label for="td_order"></label><select id="deal_select">';
            foreach ($deals as $d){
                $text .= '<option value="'.$d['id'].'">'.$d['name'].'</option>';
            }
            $text .= '</select></td>';
        } else if ($type == 'promotion') {//Lấy về khuyến mãi===============================================
            $promotion = Db_Promotion::getPromotionByHotelId($hotel);
            $text = '<th>Chọn khuyến mãi:</th><td><label for="td_order"></label><select id="deal_select">';
            foreach ($promotion as $ph){
                $text .= '<option value="'.$ph['id'].'">';
                if (($temp2 = Db_Promotion::getPromotionFilterById($ph['type'])) !== false) {
                    $ph['name_type'] = $temp2[$ph['type']]['name'];
                    $text .= $ph['name_type'].' - ';
                }
                if ($ph['room_id'] == 0) {
                    $ph['name_room'] = "Toàn bộ khách sạn";
                } else {
                    if (($temp3 = Db_Room::getRoomById($ph['room_id'])) !== false) {
                        $ph['name_room'] = "Phòng " . $temp3['name'];
                        $text .= $ph['name_room'].' - ';
                    }
                }
                $text .= '['.$ph['time_start'].' => '.$ph['time_end'].'] giá '.$ph['price'].' ('.$ph['percent'].'%)</option>';
            }
            $text .= '</select></td>';
        } else if ($type == 'room'){
            $text = '<th>Chọn phòng:</th><td><label for="td_order"></label><select id="room_select" name="room_hotel" class="allroom">';
            $room = Db_Room::getRoomsByHotelId($hotel);
            foreach ($room as $ph){
                $text .= '<option value="'.$ph['id'].'">'.$ph['name'].'</option>';
            }
            $text .= '</select></td>';
        }

        echo $text;
    }

    function setItem(){ /*Hàm sử dụng trong manage_prior_hotel - Tú 09.01.13*/
        $key = (int)Url::getInt('key');
        $val = (int)Url::getInt('val');
        $type = Url::get('type');
        if (!empty($key)){
            $result = Db_PriorHotel::updatePriorHotelById($key, $val);
            if ($result){
                if(!empty($val)){
                    if ($type == 'deal'){//Lấy về deal==================================================================
                        $deal = Db_Deal::getDealHotelById($val);
                        $hotel = Db_Hotel::getHotelById($deal['hotel_id']);
                        echo $deal['name'].' - '.$hotel['name'];
                    } else if ($type == 'hotel') {//Lấy về Khách sạn====================================================
                        $hotel = Db_Hotel::getHotelById($val);
                        $city = Db_Location::getLocaltionNameById($hotel['city_id']);
                        echo $hotel['name'].' - '.$city;
                    } else if ($type == 'promotion') {//Lấy về khuyến mãi===============================================
                        $ph = Db_Promotion::getPromotionHotelById($val);
                        $ph['time_start'] = date('d-m-Y', $ph['time_start']);
                        $ph['time_end'] = date('d-m-Y', $ph['time_end']);
                        if (($temp2 = Db_Promotion::getPromotionFilterById($ph['type'])) !== false) {
                            $ph['name_type'] = $temp2[$ph['type']]['name'];
                        }
                        if ($ph['room_id'] == 0) {
                            $ph['name_room'] = "Toàn bộ khách sạn";
                        } else {
                            if (($temp3 = Db_Room::getRoomById($ph['room_id'])) !== false) {
                                $ph['name_room'] = "Phòng " . $temp3['name'];
                            }
                        }
                        $hotel = Db_Hotel::getHotelById($ph['hotel_id']);
                        $city = Db_Location::getLocaltionNameById($hotel['city_id']);
                        echo '<p>'.$hotel['name'].' - '.$city.' ('.$ph['name_room'].')</p><p id="info_{$i.id}" class="info" style="display:block; position:static;">Bắt đầu : '.$ph['time_start'].'<br>Kết thúc : '.$ph['time_end'].'<br>Loại : '.$ph['name_type'].'<br>Giá : '.$ph['price'].' (giảm '.$ph['percent'].'%)<br></p>';
                    }
                }else{
                    echo 'Tùy chọn';
                }
            }else{
                echo false;
            }
        }
        echo false;
    }
}
?>